using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using Zhaoxi.Zhihu.Core.AppUserAggregate.Entites;
using Zhaoxi.Zhihu.Core.QuestionAggregate.Entites;
using Zhaoxi.Zhihu.SharedKerel.Paging;
using Zhaoxi.Zhihu.UseCases.Common.Queries.Interfaces;

namespace Zhaoxi.Zhihu.Infrastructure;

public class DataQueryService(AppDbContext  dbContext):IDaraQueryService
{
    public IQueryable<AppUser> AppUsers => dbContext.AppUsers;
    public IQueryable<FollowQuestion> FollowQuestions => dbContext.FollowQuestions;
    public IQueryable<FollowUser> FollowUsers => dbContext.FollowUsers;
    public IQueryable<Question> Questions => dbContext.Questions;
    public IQueryable<Answer> Answers => dbContext.Answers;
    public IQueryable<AnswerLike> AnswerLikes => dbContext.AnswerLikes;
    public async Task<T?> FirstOrDefaultAsync<T>(IQueryable<T> queryable) where T : class
    {
        return await queryable.FirstOrDefaultAsync();
    }

    public async Task<IList<T>> ToListAsync<T>(IQueryable<T> queryable) where T : class
    {
        return await queryable.ToListAsync();
    }

    public async Task<PagedList<T>> PageListAsync<T>(IQueryable<T> queryable, Pagination pagination) where T : class
    {
        var count = queryable.Count();
        var items = await queryable
            .Skip((pagination.PageNumber - 1) * pagination.PageSize)
            .Take(pagination. PageSize)
            .ToListAsync();
        
        return new PagedList<T>(items, count, pagination);
    }

    public async Task<bool> AnyAsync<T>(IQueryable<T> queryable) where T : class
    {
        return await queryable.AsNoTracking().AnyAsync();
    }
}